package com.bsoft.hospitalchs.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.model.Pictures;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 泅渡者
 * Created on 2017/5/15.
 */

public class PicturesAdapter extends BaseAdapter {
    private Context context;
    private List<Pictures> pathList ;
    private Handler myhandler;

    public PicturesAdapter(Context context, List<Pictures> pathList,Handler handler) {
        this.context = context;
        this.pathList = pathList;
        this.myhandler = handler;
    }

    @Override
    public int getCount() {
        return pathList.size();
    }

    @Override
    public Object getItem(int position) {
        return pathList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    @Override
    public int getItemViewType(int position) {
        if (pathList.get(position).type == 0) {
            return 0;
        } else {
            return 1;
        }
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final int type = getItemViewType(position);
        ViewHolder holder= null;
        ViewHolder2 holder2 = null;
        if (convertView == null) {
            switch (type) {
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.pictures_add, null);
                    holder = new ViewHolder(convertView);
                    holder.iv_add.setClickable(true);
                    holder.iv_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(pathList.size()<2){

                                Message message = new Message();
                                message.what = 1001;
                                message.arg1 = position;
                                myhandler.sendMessage(message);
                            }
                        }
                    });
                    convertView.setTag(holder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.pictures_item, null);
                    holder2 = new ViewHolder2(convertView);
                    Glide.with(context)
                            .load(pathList.get(position).path)
                            .into(holder2.iv_pic);
                    holder2.iv_pic.setClickable(false);
                    holder2.iv_delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Message message = new Message();
                            message.what = 1002;
                            message.arg1 = position;
                            myhandler.sendMessage(message);
                        }
                    });
                    convertView.setTag(holder2);
                    break;
            }
        }else{
            switch (type) {
                case 0:
                    holder = (ViewHolder) convertView.getTag();
                    holder.iv_add.setClickable(true);
                    holder.iv_add.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(pathList.size()<2){

                                Message message = new Message();
                                message.what = 1001;
                                message.arg1 = position;
                                myhandler.sendMessage(message);
                            }
                        }
                    });
                    break;
                case 1:
                    holder2 = (ViewHolder2) convertView.getTag();
                    Glide.with(context)
                            .load(pathList.get(position).path)
                            .into(holder2.iv_pic);
                    break;
            }
        }
        return convertView;
    }
    /**
     * listView中Item 的实例化视图
     */
    static class ViewHolder {
        ImageView iv_add;


        ViewHolder(View view) {
            iv_add = (ImageView) view.findViewById(R.id.iv_add);
        }



    }
    /**
     * listView中Item 的实例化视图
     */
    static class ViewHolder2 {

        ImageView iv_pic;
        ImageView iv_delete;


        ViewHolder2(View view) {
            iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
            iv_delete = (ImageView) view.findViewById(R.id.iv_delete);
        }
    }

}
